// pages/chartere_bus6/search_models6-2/search_models6-2.js
var httpUtil = require('../../utils/httpUtil.js');
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
     flag:false,
    goodsList: [],
    isShowCar:true,
    specificationList:[],
    money:0,
    date:"",
    itemData:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getList();
    var pages = getCurrentPages();

    var prevPage = pages[pages.length - 2];  //上一个页面
    this.setData({
      date: prevPage.data.date
    })
  }, getList: function () {
    var that = this;
    httpUtil.getHttp("/goods_travel/list?type=6", {}, function (res) {
      if (res != null && res.length!=0){
        for (var i = 0; i < res.length;i++){
          var list=[];
          if (res[i].label != null && res[i].label!=''){
            console.log(res[i].label);
            list = res[i].label.split(",");
           }
          res[i].labelList = list
        }
      }
      that.setData({
        goodsList: res
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  addCar:function(e){
    var id = e.target.dataset.id;
    var type = e.target.dataset.type;
    if (id == '' || id == undefined || type == '' && type == undefined){

    }
    this.getSpecification(id,type);
    this.setData({
      isShowCar: false
    })
  },
  getSpecification: function (id,type) {
    var that = this;
    httpUtil.getHttp("/goods_travel/get/" + id + "/" + type + "/specification", {}, function (res) {
      var s=0;
      var money=0;
      for (var i = 0; i < res.length; i++) {
        res[i].num=0;
        if (s==0){
          money = res[i].money;
          res[i].num = 1;
        }
       }
        that.setData({
          specificationList: res,
          money: money
        })

     })
  },
  payCar:function(e){
    var that=this;
    var id = e.target.dataset.id;
    var type = e.target.dataset.type;

    if(id==''||id==undefined){
      return;
    }
    httpUtil.getHttp("/goods_travel/get/" + id + "/" + type + "/specification", {}, function (res) {
      var shoppingList = [];
      var userInfo = util.getKey("userInfo");
      shoppingList.push({ "num": 1, "specificationId": res[0].id, "goodsId": res[0].goodsId, "userId": userInfo.id, "del_status": 1,"date":that.data.date })
      httpUtil.postHttp("/order/addShoppingList", { list: shoppingList }, function (res) {

            wx.navigateTo({
             url: '../reserve1/reserve1?id=' + res[0].id
         });
    
      });

    })

  },
  addNum:function(e){
    var that=this;
    console.log("添加");
    var index = e.target.dataset.id;
    that.data.specificationList[index].num += 1;
    that.getMoney(that.data.specificationList);
  },
  delNum: function (e) {
    var that = this;
    console.log("减少");

    var index = e.target.dataset.id;
    var num = that.data.specificationList[index].num;
    that.data.specificationList[index].num = (num - 1) < 0 ? 0 : num - 1;
    that.getMoney(that.data.specificationList);
  },
  getMoney: function (specificationList){
    var that=this;
    var money=0.0;
    console.log(specificationList);
    for (var i=0;i<specificationList.length;i++){
      var model = specificationList[i];
      if(model.num>0){
      money += model.num * model.money;
        console.log(model.num * model.money);

        console.log(money);

      }
    }
    that.setData({
      specificationList: specificationList,
      money: money
    })
  },
  helpBack:function(){
    wx.navigateTo({
      url: '../back_know/back_know'
    })
  },
   showDatail:function(){
    var flag=this.data.flag;
    this.setData({
      flag:!flag
    })
  },
  hide:function(){
    this.setData({
      isShowCar: true
    })
  },
  addShopping:function(){
    var that=this;
    var shoppingList=[];
    var userInfo = util.getKey("userInfo");

    var specificationList = that.data.specificationList;
    for (var i = 0; i < specificationList.length; i++) {
      var model = specificationList[i];
      if (model.num > 0) {
        shoppingList.push({ "num": model.num, "specificationId": model.id, "goodsId": model.goodsId, "userId": userInfo.id, "del_status": 0, "date": that.data.date })

      }
    }
    if (shoppingList.length == 0) {
         wx.showToast({
          title: '请选择服务',
          icon: 'loading',
          duration: 1000,
          mask: true
        })
        return;
        }
    httpUtil.postHttp("/order/addShoppingList", { list: shoppingList},function(res){
      console.log(res);
      var ids="";
      for(var i=0;i<res.length;i++){
        if (i!=0){
          ids+=","
        }

        ids+=res[i].id;
      }
      wx.navigateTo({
        url: '../reserve1/reserve1?id=' + ids
      });
   
    });
    console.log(shoppingList);
  },

  showMore:function(e){
    var curr = e.currentTarget.dataset.index;
    this.setData({
      itemData: curr
    })
  }
})